<template>
	<div id="container">
		<row class="header">
	        <ICol span="24"><h3>登陆系统</h3></ICol>
	    </row>
	    <row class="body">
	        <IForm ref="form" :model="form" :rules="formRule" >
	            <FormItem prop="user">
	                <IInput v-model="form.user"  size="large" placeholder="用户名" />
	            </FormItem >
	            <FormItem prop="password">
	                <IInput v-model="form.password" type="password"  size="large" placeholder="密码" />
	            </FormItem>
	            <FormItem>
	            	<IButton @click="handleSubmit('form')" type="primary" long v-bind:disabled="btn.disabled">{{btn.txt}}</IButton>
	            </FormItem>
	        </IForm>
	    </row>
	</div>
</template>
<script>
	import {Row,Col} from 'iview/src/components/grid';
	import Form from 'iview/src/components/form';
	import Input from 'iview/src/components/input';
	import Button from 'iview/src/components/button';
	import Axios from 'admin/config/axios';

	export default {
		components:{
            Row,
			'ICol' : Col,
			'IInput' : Input,
			'IForm' : Form,
			'FormItem' : Form.Item,
			'IButton' : Button,
        },
		data (){
	        return{
	        	btn : {
	        		txt:'登录',
	        		disabled : false
	        	},
	        	form : {
	        		user : '',
	        		password : ''
	        	},
	        	formRule : {
	                user: [
	                    { required: true, message: '请填写用户名', trigger: 'blur' },
	                    { type: 'string', min: 3, max:15, message: '用户名3-15位', trigger: 'blur' }
	                ],
	                password: [
	                    { required: true, message: '请填写密码', trigger: 'blur' },
	                    { type: 'string', min: 6, max:15, message: '密码长度6-15位', trigger: 'blur' }
	                ]
	            },
	        }
    	},
        methods: {
	        handleSubmit(name) {
	            this.$refs[name].validate((valid) => {
	                if (!valid) {return false;}
	                this.btn.txt='登录中..';
	                this.btn.disabled = true;
	                var _this = this;
	                Axios.Login(this.form)
	                .then(res =>{
	            		if(!res) return false;
	                    if(res.code==1){
	                        _this.btn.disabled = false;_this.btn.txt = '登录';
	                        return false;
	                    }
	                    setTimeout(function(){window.location.href="/admin/#";},1000);
	                })
	            })
	        },
	    },
	    created:function(){
	    	var _this  = this;
	    	window.onkeydown = function(){
	    		var key=window.event.keyCode;
	    		if(key==13){
	    			_this.handleSubmit('form');
	    		}
	    	}
	    }
	}
</script>